﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Tong;
public partial class Menu_Menu_Edit : System.Web.UI.Page
{
    public TongUse TextData = new TongUse();
    /// <summary>
    /// 页面输出的HTML代码 下拉菜单的数据和值
    /// </summary>
    public string SelctText="";
    
    /// <summary>
    /// 用户的ＩＤ
    /// </summary>
    string id;

    /// <summary>
    /// 得到当前的Tree样的菜单
    /// </summary>
    Tong.TongTreeDataTable TextTreeTable = new TongTreeDataTable();
    protected void Page_Load(object sender, EventArgs e)
    {

        try
        
        {
             id = Request.QueryString["id"].ToString();

            if (IsPostBack)
            {
                if (DownList.Value != "")
                {
                    Response.Write(DownList.Value);
                   ThisPageSave(int.Parse(DownList.Value));
                }
                
         
                
            }

            DataTable ListTable = TextData.SqlataSet("select * from [TreeClass] where id=" + id + "").Tables[0];

            
            SortList(0);

            Ttv.Text = "编辑类别" + ListTable.Rows[0]["ClassName"].ToString() + "的资料";
            intro.Value = ListTable.Rows[0]["intro"].ToString();

            ClassName.Value = ListTable.Rows[0]["ClassName"].ToString();
            SortNum.Value = ListTable.Rows[0]["SortNum"].ToString();
            URL.Value = ListTable.Rows[0]["URL"].ToString();

            Page.Title = "编辑类别" + ListTable.Rows[0]["ClassName"].ToString() + "的资料"; //页面标题

            ListTable.Clear();//清空变量

            DownList.DataSource = TextData.SqlataSet("select * from [TreeClass]  order by SortNum").Tables[0].DefaultView;
            DownList.DataSource = TextTreeTable.TreeTable("TreeClass", "ID", "ID", "ClassName", "followid");

            
            DownList.DataTextField = "Name";
            DownList.DataValueField = "Value";
           
            DownList.DataBind();
            DownList.Value = id;
        
        }
        catch 
        {
            
        }


        
        
        
    }

    protected void SortList(int PraID)
    {
       
        string id =  Request.QueryString["id"].ToString();
        int ii = 0;
        string sql;
        
        sql = "select * from [TreeClass] where followid=" + PraID + "order by SortNum";//SQL查询语句

        DataTable MyDt;
        MyDt = TextData.SqlataSet(sql).Tables[0];

        
        for (int i = 0; i < MyDt.Rows.Count; i++)
        {






            //if (MyDt.Rows[i]["id"].ToString() == id)
           // {

               // SelctText = MyDt.Rows[i]["id"].ToString()+ MyDt.Rows[i]["className"].ToString();
               // SelctText += "<option value=\" " + MyDt.Rows[i]["id"].ToString() + " \"  selected>" + TextData.EStr(i, "　") + MyDt.Rows[i]["className"].ToString() + " </option>\n";//显示数据
           // }
           // else
           // {
            SelctText+= MyDt.Rows[i]["id"].ToString() + "Ж" + TextData.EStr(i, "　") + MyDt.Rows[i]["className"].ToString()  ;
            
              //  SelctText += "<option value=\" " + MyDt.Rows[i]["id"].ToString() + " \" >" + TextData.EStr(i, "　") + MyDt.Rows[i]["className"].ToString() + " </option>\n";//显示数据
           // }
            ii += 1;
            SortList(int.Parse(MyDt.Rows[i]["id"].ToString()));
            ii -= 1;
        }
        
    }

    /// <summary>
    /// 頁面資料保存
    /// </summary>
    protected void ThisPageSave(int sid)
    {
        string sql = "select top 1 from [TreeClass]";

        int SelVal = sid;
            try
            {
                SelVal = int.Parse(Request.Form[8].ToString());//页面数据select 值
            }
            catch
            { 
            
            }
            if (int.Parse(id) == SelVal)//如果没有选择或还是以前的那个ID
            {
                sql = "update [TreeClass] Set intro='" + intro.Value.Trim() + "',ClassName = '" + ClassName.Value.Trim() + "'" +
                    ",SortNum = " + SortNum.Value.Trim() + "," +
                "URL = '" + URL.Value.Trim() + "' where id = " + id;

            }
            else
            {

                sql = "update [TreeClass] Set followid=" + SelVal + ",  intro='" + intro.Value.Trim() + "',ClassName = '" + ClassName.Value.Trim() + "',SortNum = " + SortNum.Value.Trim() + ",URL = '" + URL.Value.Trim() + "'where id = " + id;
               
            }


            TextData.SqlDataSave(sql);//执行SQL 更新数据
            //Response.Write(sql + "//" );
            TextData.Res.Response.Redirect("Menu_Show.aspx");

           

    }
}
